package com.carrentalssm.controller;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.carrentalssm.entity.User;
import com.carrentalssm.service.UserService;

/**
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 注册用户
	 * @param username
	 * @param password
	 * @param password2
	 * @param phone
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(
			@RequestParam("username") String username,
			@RequestParam("password") String password,
			@RequestParam("password2") String password2,
			@RequestParam("phone") String phone,
			Model model) {
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password2);
		user.setPhone(phone);
		user.setCreateDatetime(new Timestamp(new Date().getTime()));
		
		System.out.println("添加用户：" + user);
		
		if (!password.equals(password2)) {
			System.out.println("添加用户：密码不一致，" + user);
			model.addAttribute("errorMsg", "密码不一致");
			return "register";
		} else {
			try {
				User selectByUsername = userService.selectByUsername(username);
				if (selectByUsername != null) {
					System.out.println("添加用户：用户名已存在，" + user);
					model.addAttribute("errorMsg", "用户名已存在");
					return "register";
				} else {
					userService.add(user);
					model.addAttribute("successMsg", "注册成功，去登录吧");
					return "register";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		model.addAttribute("errorMsg", "注册失败");
		return "register";
	}
	
	/**
	 * 注册用户
	 * @param username
	 * @param password
	 * @param password2
	 * @param phone
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(
			@RequestParam("username") String username,
			@RequestParam("password") String password,
			@RequestParam("password2") String password2,
			@RequestParam("phone") String phone,
			Model model,
			HttpServletRequest request) {
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password2);
		user.setPhone(phone);
		user.setCreateDatetime(new Timestamp(new Date().getTime()));
		
		System.out.println("修改用户：" + user);
		
		if (!password.equals(password2)) {
			System.out.println("修改用户：密码不一致，" + user);
			model.addAttribute("errorMsg", "密码不一致");
			return "userUpdate";
		} else {
			try {
				userService.update(user);
				request.getSession().setAttribute("user", userService.selectByUsername(username));
				model.addAttribute("successMsg", "修改成功");
				return "userUpdate";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		model.addAttribute("errorMsg", "修改失败");
		return "userUpdate";
	}
	
	/**
	 * 删除用户
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(String username) {
		userService.delete(username);
		return "redirect:/adminUserList.jsp";
	}

}
